/*
 *	Source: http://www.yolinux.com/TUTORIALS/XML-Xerces-C.html
 */

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <list>


#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include "XMLParser.h"


using namespace xercesc;
using namespace std;



/**
 *  - Tests the access and availability of the XML configuration file.
 *  - Configures the xerces-c DOM parser.
 *  - Reads and extracts the pertinent information from the XML config file.
 * @param const char * configFile - filename
 * @param std::vector<Map*>& vecResult - information is stored here
 * @return void
 */
void XMLParser::readMapConfigFile(const char * configFile, std::vector<Map*>& vecResult)
{
	try
	{
		if (checkFile(configFile) == -1)
		{
			throw(std::runtime_error( "XML document not available" ));
		}
		m_ConfigFileParser->parse( configFile );
		DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
		DOMElement* elementRoot = xmlDoc->getDocumentElement();
		if( !elementRoot )
		{
			throw(std::runtime_error( "empty XML document" ));
		}
		if (!XMLString::equals(elementRoot->getTagName(), m_TAG_root))
		{
			throw(std::runtime_error( "Bad root tag for this config file." ));
		}
		DOMNodeList* childrens = elementRoot->getElementsByTagName(m_TAG_ApplicationSettings);
		const  XMLSize_t nodeCount = childrens->getLength();
		for( XMLSize_t i = 0; i < nodeCount; ++i )
		{
			DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >(childrens->item(i));
			if (m_ATTR_Options.size() == XMLPARSER_NUMOPTIONS_MAPCONFIG)
			{
				Map* newMap = new Map;
				const XMLCh* xmlch_OptionA = currentElement->getAttribute(m_ATTR_Options[0]);
				const XMLCh* xmlch_OptionB = currentElement->getAttribute(m_ATTR_Options[1]);
				const XMLCh* xmlch_OptionC = currentElement->getAttribute(m_ATTR_Options[2]);
				const XMLCh* xmlch_OptionD = currentElement->getAttribute(m_ATTR_Options[3]);
		
				newMap->setName(XMLString::transcode(xmlch_OptionA));
				newMap->setID(XMLString::transcode(xmlch_OptionB));
				newMap->setMapDimension(atoi(XMLString::transcode(xmlch_OptionC)),
										atoi(XMLString::transcode(xmlch_OptionD)));
				DOMNodeList* childrens2 = currentElement->getElementsByTagName(m_TAG_ApplicationSettings2);
				const  XMLSize_t nodeCount2 = childrens2->getLength();
				for( XMLSize_t j = 0; j < nodeCount2; ++j )
				{
					DOMElement* currentElement2 = dynamic_cast< xercesc::DOMElement* >(childrens2->item(j));
					MapCell newCell;	
					const XMLCh* xmlch_OptionE = currentElement2->getAttribute(m_ATTR_Options[4]);
					const XMLCh* xmlch_OptionF = currentElement2->getAttribute(m_ATTR_Options[5]);
					const XMLCh* xmlch_OptionG = currentElement2->getAttribute(m_ATTR_Options[6]);
					const XMLCh* xmlch_OptionH = currentElement2->getAttribute(m_ATTR_Options[7]);
					const XMLCh* xmlch_OptionI = currentElement2->getAttribute(m_ATTR_Options[8]);
					newCell.activate();
					newCell.setType(XMLString::transcode(xmlch_OptionG));
					newCell.setContent(XMLString::transcode(xmlch_OptionH));
					newCell.setDesc(XMLString::transcode(xmlch_OptionI));
					newMap->setCell(atoi(XMLString::transcode(xmlch_OptionE)),
									atoi(XMLString::transcode(xmlch_OptionF)),
									newCell);
				}
				vecResult.push_back(newMap);
			}
		}
	}
	catch( xercesc::XMLException& e )
	{
		char* message = xercesc::XMLString::transcode( e.getMessage() );
		ostringstream errBuf;
		errBuf << "Error parsing file: " << message << flush;
		XMLString::release( &message );
	}
}